#-*- coding: utf-8 -*-
from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User


# Create your models here.

gender_list = (('M', 'Male'), ('F', 'Female' ))
class Blog(models.Model):
    title = models.CharField('Title', max_length=200)
    text = models.TextField('Text', max_length=2048)
    date = models.DateTimeField('Last Modified' )
    
    def blog_size (self):
        return len(self.text)

    
    def __str__(self):
        return '%s' % self.title
    
    class Meta:
        permissions=(
            ('can_blog','Allowed to Blog'),
                     )

class Person(models.Model):
    """Classe pessoa do livro django 24 horas
    """
    userID = models.ForeignKey(User, unique = True)
    name = models.CharField('name', max_length=200)
    email = models.EmailField('Email', unique = True)
    birthday = models.DateField('Birthday', blank=True, null=True)
    gender = models.CharField(max_length=1 , choices=gender_list)
    favoriteURL = models.URLField('myURL')
    headshot = models.ImageField(upload_to='img', blank = True)
    text = models.TextField('Desc', max_length=500,blank = True)
    friends = models.ManyToManyField('self' , blank=True)
    blogs = models.ManyToManyField( Blog , blank=True)
    
    def __unicode(self):
        return self.name
    
    def __str__(self):
        return '%s' % (self.name)
    
    
class BlogAdmin(admin.ModelAdmin):
    list_display = ('title', 'date', 'blog_size')
    list_filter = ('date',)
    date_hierarchy  = 'date'
    search_fields = ('title', 'text',)



class PersonAdmin(admin.ModelAdmin):
     list_display = ('name', 'email',)
     list_filter = ('gender', 'friends',)
     ordering    = ('-name',)
     fieldsets = (
        ('User', {'fields':('userID', 'name')}),
        ('Info', {'fields':('birthday', 'gender')}),
        ('Contact', {'fields':('email', 'favoriteURL')}),
        ('Amigos',{'fields':['friends'], 'classes':['collapse']}),
        ('Blogs', {'fields':['blogs'], 'classes': ['collapse']}),

 
               )




admin.site.register(Person,PersonAdmin)
admin.site.register(Blog,BlogAdmin)